package com.service.business.rabbitmq.listener;


import com.beust.jcommander.internal.Lists;
import com.service.business.base.dto.LoginUserSession;
import com.service.business.base.service.ConfigService;
import com.service.business.member.service.MemberStoreService;
import com.service.business.merchant.service.MerchantCreditLineService;
import com.service.business.merchant.service.MerchantOrderService;
import com.service.business.merchant.support.WriteOffOrder;
import com.service.business.rabbitmq.binding.WriteOffOrderSink;
import com.service.business.support.Language;
import com.service.business.support.MemberMerchantLockUtils;
import com.service.business.support.MemberStoreLockUtils;
import com.service.business.utils.I18nUtil;
import com.service.core.exception.CommonException;
import com.service.dao.constants.ConfigConstants;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.MerchantOrderEnums;
import com.service.dao.model.MemberStore;
import com.service.dao.model.MerchantCreditLine;
import com.service.dao.model.MerchantOrder;
import com.service.dao.model.SysConfig;
import lombok.extern.slf4j.Slf4j;
import org.redisson.RedissonRedLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Component
//@EnableBinding(WriteOffOrderSink.class)
public class WriteOffOrderReceiverListener {

    @Autowired
    private ConfigService configService;
    @Autowired
    private MemberStoreService storeService;
    @Autowired
    private MemberStoreLockUtils storeLockUtils;
    @Autowired
    private MerchantOrderService merchantOrderService;
    @Autowired
    private MerchantCreditLineService creditLineService;

    /**
     * @return void 返回类型
     * @throws
     * @Title: push_depth
     * @Description: TODO(订单变化)
     */
//    @StreamListener(WriteOffOrderSink.IN_WRITE_OFF_ORDER)
    public void receiveWriteOffOrder(WriteOffOrder writeOffOrder) {
        log.info("收到创建核销订单消息:{}", writeOffOrder);

        MerchantOrder order = merchantOrderService.getById(writeOffOrder.getOrderId());
        if (ObjectUtils.isEmpty(order)) {
            log.info("订单<{}>核销失败:不存在此订单", writeOffOrder);
            return;
        }
        if (ObjectUtils.nullSafeEquals(order.getOrderStatus(), MerchantOrderEnums.Status.write_off.getCode())) {
            log.info("订单<{}>核销失败:此订单已核销", writeOffOrder);
            return;
        }


        SysConfig creditCofing = configService.getConfigByName(ConfigConstants.MERCHANT_CREDIT_LINE_COINS);
        CoinsEnum coins = CoinsEnum.valueOf(creditCofing.getConfOptions());

        MemberStore exdfStore = storeService.getMemberStore(writeOffOrder.getMerchantMemberId(), coins.getName());
        MerchantCreditLine creditLine = creditLineService.getCreditLine(order.getMerchantId(), coins);

        BigDecimal totalNum = exdfStore.getNum().add(creditLine.getCreditNum());
        if (totalNum.compareTo(order.getTotalConcessionNum()) < 0) {
            String msg = I18nUtil.getMessage("merchant_order_exception_store_not_enough", Language.LANG.get(), CoinsEnum.EXDF.getName());
            throw new CommonException(msg);
        }

//        List<MemberStore> storeList = Lists.newArrayList(
//                exdfStore,
//                storeService.getMemberStore(writeOffOrder.getMerchantMemberId(), CoinsEnum.MU.getName()),
//                storeService.getMemberStore(order.getMemberId(), CoinsEnum.MU.getName())
//        );
//        List<String> lockKeyList = MemberStoreLockUtils.buildStoreKey2List(storeList);
//        lockKeyList.add(MemberMerchantLockUtils.buildOrderLock(order.getOrderNo()));
//        lockKeyList.add(MemberMerchantLockUtils.buildCreditLineLock(order.getMerchantId(), coins.getName()));
//        RedissonRedLock redLock = storeLockUtils.getRedLock(lockKeyList);
//        try {
//            if (storeLockUtils.redLockTryLock(redLock)) {
//                merchantOrderService.writeOff(order, writeOffOrder.getMerchantMemberId(), exdfStore, creditLine);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            Language.LANG.set("zh_CN");
//            log.info("订单<{}>核销失败:{}", writeOffOrder, e.getMessage());
//        } finally {
//            redLock.unlock();
//        }

    }

}
